package cn.com.gridinfo.sooc.core.expand.feedback.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.feedback.CodeConst;
import cn.com.gridinfo.sooc.core.expand.feedback.dao.SuggestionDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 意见反馈
 * Created by yingchunli on 2015/12/22.
 */
public class SummitSuggestionFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.commitBtn_feedback)
    Button commitBtn;//提交按钮
    @InjectView(R.id.feedback)
    EditText feedback;//意见内容
    SuggestionDao dao;

    public static SummitSuggestionFragment newInstance(String info) {
        return new SummitSuggestionFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_feedback_suggestion, null);
        ButterKnife.inject(this, view);
        dao = new SuggestionDao(this);
        return view;
    }

    /**
     * 提交按钮的点击
     */
    @OnClick(R.id.commitBtn_feedback)
    void onCommitClick() {
        //提交反馈
        dao.summitSuggestion(feedback.getText().toString().trim());
    }

    /**
     * 判断输入框内容是否为空，为空时，提交按钮不可点击，否则可点击
     */
    @OnTextChanged(value = {R.id.feedback},
            callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (feedback.getText().toString().trim().equals("")) {
            commitBtn.setEnabled(false);
        } else {
            commitBtn.setEnabled(true);
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            switch (requestCode) {
                case CodeConst.EXPAND_FEEDBACK_SUMMIT:
                    //提交反馈
                    if (dao.getStatus().equals("0")) {
                        MessageUtils.showRightImageShortToast(getActivity(), "提交成功");
                        feedback.setText("");
                        feedback.setHint("我们很重视您的宝贵建议和反馈");
                    } else {
                        MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
                    }
                    break;
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "数据请求异常");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
